package com.inspur.his.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.inspur.his.pojo.Bing;

import com.inspur.his.service.BingAnService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

@Controller
public class BingAnController {

    @Autowired
    BingAnService binganService;

    @RequestMapping("BingAnController")//访问地址
    public String BingAnList(HttpServletRequest request, Model model, @RequestParam(value = "pn", defaultValue = "1") Integer pn) throws UnsupportedEncodingException {
        //1.设置字符编码集
        request.setCharacterEncoding("UTF-8");
        //获取页面提交来的查询条件，来自前端页面查询部分标签的 name属性
        //String membername = request.getParameter("patientName");//病人姓名
        String patientid = request.getParameter("FgNum");//病案号
        //  点击用户管理左侧菜单进行默认无条件查询
        HashMap map = new HashMap();
        //map.put("membername",membername);
        map.put("patientid",patientid);
        PageHelper.startPage(pn,5);
        List<Bing> binganList = binganService.binganInList(map);
        //*这里pageInfo.list就能获得分页完的数据*//*
        PageInfo<Bing> pageInfo = new PageInfo<Bing>(binganList,binganList.size());
        //*Model主要用于传值给jsp*//*
        model.addAttribute("binganList",binganList);
        model.addAttribute("pageInfo",pageInfo);
        //model.addAttribute("smembername",membername);
        model.addAttribute("patientid",patientid);

        return "BingAn/InformationList.jsp";
    }

    //初始先跳到save页面
    @RequestMapping("BingAnInitInsertController")//访问地址
    public String BingAnInitInsert(HttpServletRequest request,Model model) throws UnsupportedEncodingException {
        //1.设置字符编码集
        request.setCharacterEncoding("UTF-8");
        //跳转到添加页面
        return "BingAn/InformationAdd.jsp";
    }
    //病案添加
    @RequestMapping("BingAnSaveController")//访问地址
    public String BingAnSave(HttpServletRequest request, HttpServletResponse response) throws IOException, ParseException {
        //1.设置字符编码集
        request.setCharacterEncoding("UTF-8");
        //2.接受页面提交的数据
        String patientid = request.getParameter("comNo");//病案id
        String timeid = request.getParameter("comName");//入院次数
        String name = request.getParameter("comAddress");//真实姓名
        String credit = request.getParameter("contactTel");//身份证
        String address = request.getParameter("contactPer");//地址
        String tel = request.getParameter("comType");//电话

        //将接受的数据封装
            Bing bing =new Bing();
        bing.setPatientid(patientid);
        bing.setTimeid(timeid);
        bing.setName(name);
        bing.setCredit(credit);
        bing.setAddress(address);
        bing.setTel(tel);

        //调用service保存
        //这里不要new新的service层，会导致mapper接口null
        binganService.save(bing);

        return "redirect:BingAnController";
    }

    //修改信息
    @RequestMapping("BingAnFindByIdController")//访问地址
    public String BingAnFindById(HttpServletRequest request,Model model) throws ParseException, UnsupportedEncodingException {
        //1.设置字符编码集
        request.setCharacterEncoding("UTF-8");
        //获取目标病人id
        String id = request.getParameter("id");//病案ID
        Bing bing =new Bing();
        bing.setPatientid(id);

        Bing bing2 = binganService.findById(bing);//
        /*Member member2 = memberService.findById(member);//id是主键id*/

        model.addAttribute("bing2",bing2);

        return "BingAn/InformationUpdate.jsp";
    }
    @RequestMapping("BingAnModifyController")//访问地址
    public String BingAnModify(HttpServletRequest request,Model model) throws ParseException, UnsupportedEncodingException {
        //1.设置字符编码集
        request.setCharacterEncoding("UTF-8");
        //获取信息
        String patientid = request.getParameter("comNo");//病案id
        String timeid = request.getParameter("comName");//入院次数
        String name = request.getParameter("comAddress");//真实姓名
        String credit = request.getParameter("contactTel");//身份证
        String address = request.getParameter("contactPer");//地址
        String tel = request.getParameter("comType");//电话

        //将接受的数据封装
        Bing bing =new Bing();
        bing.setPatientid(patientid);
        bing.setTimeid(timeid);
        bing.setName(name);
        bing.setCredit(credit);
        bing.setAddress(address);
        bing.setTel(tel);
        //调用service更新
        binganService.binganUpdate(bing);//Update是修改信息用的

        return "redirect:BingAnController";
    }
    @RequestMapping("BingAnDeleteController")//访问地址
    public String BingAnDelete(HttpServletRequest request,Model model) throws ParseException, UnsupportedEncodingException {
        //1.设置字符编码集
        request.setCharacterEncoding("UTF-8");
        //获取目标病人id
        String id = request.getParameter("id");//病案ID
        Bing bing =new Bing();
        bing.setPatientid(id);

        binganService.delete(bing);//
        /*Member member2 = memberService.findById(member);//id是主键id*/

        return "redirect:BingAnController";
    }

 /*   @RequestMapping("BingAnList")
    @ResponseBody
    public List<Bing> deptInfo(HttpServletRequest req, Model md) {
        HashMap map = new HashMap<String, String>();
        String departid = req.getParameter("departid");
        map.put("departid", departid);
        List<DeptExt> deptList = Service.deptList(map);
        md.addAttribute("deptList", deptList);
        return deptList;
    }*/



   /* //这里调用service类
    @Autowired


    //入院的条件查询+分页
    @RequestMapping("BingAnController")//访问地址
    public String BingAnList(HttpServletRequest request, Model model, @RequestParam(value = "pn", defaultValue = "1") Integer pn) throws UnsupportedEncodingException {
        //1.设置字符编码集
        request.setCharacterEncoding("UTF-8");
        //获取页面提交来的查询条件，来自前端页面查询部分标签的 name属性
        //String membername = request.getParameter("patientName");//病人姓名
        String patientid = request.getParameter("staffNo");//医疗证号
        //  点击用户管理左侧菜单进行默认无条件查询
        HashMap map = new HashMap();
        //map.put("membername",membername);
        map.put("patientid",patientid);
        PageHelper.startPage(pn,5);
        List<Bing> binganList = binganService.binganInList(map);
        *//*这里pageInfo.list就能获得分页完的数据*//*
        PageInfo<Bing> pageInfo = new PageInfo<Bing>(binganList,binganList.size());
        *//*Model主要用于传值给jsp*//*
        model.addAttribute("binganList",binganList);
        model.addAttribute("pageInfo",pageInfo);
        //model.addAttribute("smembername",membername);
        model.addAttribute("patientid",patientid);
        //这里上面其实是多余，因为页面是嵌套的，所以获取不到model，所以用session
        HttpSession s = request.getSession(true);
        s.setAttribute("binganList",binganList);
        s.setAttribute("pageInfo",pageInfo);
        //s.setAttribute("smembername",membername);
        s.setAttribute("patientid",patientid);

        return "BingAn/InformationList.jsp";
    }
    //初始先跳到save页面
    @RequestMapping("MemberInitInsertController")//访问地址
    public String MemberInitInsert(HttpServletRequest request,Model model) throws UnsupportedEncodingException {
        //1.设置字符编码集
        request.setCharacterEncoding("UTF-8");
        //跳转到添加页面
        return "ZhuYuan/Memberadd.jsp";
    }
    //病人添加
    @RequestMapping("MemberSaveController")//访问地址
    public String MemberSave(HttpServletRequest request, HttpServletResponse response) throws IOException, ParseException {
        //1.设置字符编码集
        request.setCharacterEncoding("UTF-8");
        //2.接受页面提交的数据
        String MEMBERID = request.getParameter("patIentno");
        String CREATEDATE = request.getParameter("inDate");
        String REALNAME = request.getParameter("patientName");
        String SEX = request.getParameter("patientSex");
        String CREDIT = request.getParameter("capacityNo");
        String MEDCARDID = request.getParameter("staffNo");
        String address = request.getParameter("homeAdder");
        String TEL = request.getParameter("heTel");
        //将接受的数据封装
        Member member =new Member();
        member.setMemberid(MEMBERID);
        member.setCreatedate(new Date());
        member.setRealname(REALNAME);
        member.setSex(SEX);
        member.setCredit(CREDIT);
        member.setMEDCARDID(MEDCARDID);
        member.setAddress(address);
        member.setTel(TEL);
        member.setStatus("0");

        //调用service保存
        //这里不要new新的service层，会导致mapper接口null
        memberService.save(member);

        return "redirect:MemberListController";
    }
    //办理入院(现根据id查询，再根据id修改,最后跳到列表页面)
    @RequestMapping("MemberInController")//访问地址
    public String MemberIn(HttpServletRequest request) throws UnsupportedEncodingException {
        //1.设置字符编码集
        request.setCharacterEncoding("UTF-8");
        //获取目标病人id
        String medcardId = request.getParameter("id");//病人MEDCARDID
        //找到并修改status为1（表示住院了）
        //条件对象
        Member member =new Member();
        member.setMEDCARDID(medcardId);
        //调用查找
        Member member2 = memberService.findById(member);
        //根据返回的对象，再修改
        memberService.updateMember(member2);

        return "redirect:MemberListController";
    }
    //修改信息
    @RequestMapping("MemberFindByIdController")//访问地址
    public String MemberFindById(HttpServletRequest request,Model model) throws ParseException, UnsupportedEncodingException {
        //1.设置字符编码集
        request.setCharacterEncoding("UTF-8");
        //获取目标病人id
        String id = request.getParameter("id");
        Member member =new Member();
        member.setMemberid(id);

        Member member2 = memberService.findById(member);//id是主键id
        *//*Member member2 = memberService.findById(member);//id是主键id*//*

        model.addAttribute("member2",member2);

        return "ZhuYuan/Memberupdate.jsp";
    }
    @RequestMapping("MemberModifyController")//访问地址
    public String MemberModify(HttpServletRequest request,Model model) throws ParseException, UnsupportedEncodingException {
        //1.设置字符编码集
        request.setCharacterEncoding("UTF-8");
        //获取目标病人id
        String id = request.getParameter("id");
        //获取信息
        String MEMBERID = request.getParameter("patIentno");
        String createdate = request.getParameter("inDate");
        String REALNAME = request.getParameter("patientName");
        String SEX = request.getParameter("patientSex");
        String CREDIT = request.getParameter("capacityNo");
        String MEDCARDID = request.getParameter("staffNo");
        String address = request.getParameter("homeAdder");
        String TEL = request.getParameter("heTel");
        System.out.println(createdate);//这里时间是cst类型
        //封装成对象
        Member member =new Member();
        member.setMemberid(MEMBERID);

        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
        //java.util.Date对象，先从字符串转化为date
        Date date = (Date) sdf.parse(createdate);
        //转化下形式
        String formatStr2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
        //转换为date类型
        member.setCreatedate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(formatStr2));
        member.setRealname(REALNAME);
        member.setSex(SEX);
        member.setCredit(CREDIT);
        member.setMEDCARDID(MEDCARDID);
        member.setAddress(address);
        member.setTel(TEL);
        //调用service更新
        memberService.memberDetailUpdate(member);//memberUpdate是修改信息用的

        return "redirect:MemberListController";
    }

    //出院的条件查询+分页
    @RequestMapping("MemberOutListController")//访问地址
    public String MemberOutList(HttpServletRequest request, Model model, @RequestParam(value = "pn", defaultValue = "1") Integer pn) throws UnsupportedEncodingException {
        //1.设置字符编码集
        request.setCharacterEncoding("UTF-8");
        //获取页面提交来的查询条件，来自前端页面查询部分标签的 name属性
        String membername = request.getParameter("patientName");//病人姓名
        String medcardid = request.getParameter("staffNo");//医疗证号
        //  点击用户管理左侧菜单进行默认无条件查询
        HashMap map = new HashMap();
        map.put("membername",membername);
        map.put("medcardid",medcardid);
        PageHelper.startPage(pn,5);
        List<Member> memberList = memberService.memberOutList(map);
        System.out.println(memberList);
        *//*这里pageInfo.list就能获得分页完的数据*//*
        PageInfo<Member> pageInfo = new PageInfo<Member>(memberList,memberList.size());
        *//*Model主要用于传值给jsp*//*
        model.addAttribute("memberList",memberList);//未分页数据
        model.addAttribute("pageInfo",pageInfo);//分页的数据
        model.addAttribute("smembername",membername);//查询条件返回
        model.addAttribute("smedcardid",medcardid);//查询条件返回
        HttpSession s = request.getSession(true);
        s.setAttribute("memberList",memberList);
        s.setAttribute("pageInfo",pageInfo);
        s.setAttribute("smembername",membername);
        s.setAttribute("smedcardid",medcardid);

        return "ZhuYuan/MemberOList.jsp";
    }
    //办理出院(现根据id查询，再根据id修改,最后跳到列表页面)
    @RequestMapping("MemberOutController")//访问地址
    public String MemberOut(HttpServletRequest request, Model model) throws UnsupportedEncodingException {
        //1.设置字符编码集
        request.setCharacterEncoding("UTF-8");
        //获取目标病人id
        String medcardId = request.getParameter("id");//病人MEDCARDID
        //找到并修改status为0（表示出院了）
        //条件对象
        Member member =new Member();
        member.setMEDCARDID(medcardId);
        //调用查找
        Member member2 = memberService.findById(member);
        member2.setDeleteDate(new Date());
        //然后跳转到确认页面
        model.addAttribute("member2",member2);//将目标对象信息传过去

        return "ZhuYuan/MemberoutDetail.jsp";
    }
    //出院确认按钮
    @RequestMapping("MemCheckOKController")//访问地址
    public String MemCheckOK(HttpServletRequest request, Model model) throws UnsupportedEncodingException {
        //1.设置字符编码集
        request.setCharacterEncoding("UTF-8");
        //获取目标病人id
        String MEDCARDID = request.getParameter("staffNo");//病人MEDCARDID
        //获取条件对象
        Member member =new Member();
        member.setMEDCARDID(MEDCARDID);
        System.out.println(member);
        //修改status为0（表示出院了）
        memberService.Memberupdate(member);//调用修改status为0的方法

        return "redirect:MemberOutListController";
    }*/
}
